home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / arg.zip / ARG.HPP < prev    next >
Text File  |  1991-02-15  |  6KB  |  156 lines

  1. #ifndef FLAG_HPP
  2. #define FLAG_HPP
  3.  
  4. /****************************************************************************\
  5.  
  6.                          Copyright (C) 1991
  7.                          Joe Kleinwaechter
  8.  
  9.       This file remains the property of Joe Kleinwaechter and may be
  10.       used freely at no charge. It may not however be resold by anyone or
  11.       included in a Class Library package without my written consent.
  12.  
  13.  
  14.  
  15.  File:         arg.hpp
  16.  
  17.  Description:  This is the header file for those that wish to wish to use
  18.                the command line parmeters class - arg.
  19.  
  20.  Usage:        The command line parameters class 'arg' is used to be able to
  21.                quickly parse off command line switches in a non-order dependent
  22.                manner. The command line parsed is of the form:
  23.  
  24.                c:>progname +s -q +tHello name
  25.  
  26.                where:
  27.                   progname is the name of the program.
  28.                   +n is an nonvalued switch, of label 'n', that has been SET
  29.                   -n is an nonvalued switch, of label 'n', that has been CLEARED
  30.                   +nVALUE is a valued switch of label 'n', that has been SET
  31.                            and has 'VALUE' as its associated text.
  32.                   -nVALUE is a valued switch of label 'n', that has been CLEARED
  33.                            and has 'VALUE' as its associated text.
  34.                   name is an unlabeled switch having 'NAME' as its associated
  35.                            text.
  36.  
  37.  Example:      #include <stream.hpp>
  38.                #include "arg.hpp"
  39.  
  40.                main(int argc, char *argv[])
  41.                {
  42. (1)            arg parm(argc,argv,1);
  43. (2)            char value[80];
  44.                argState status;
  45.                
  46. (3)               argState = parm.isSet('t',value);
  47.  
  48.                   if (status == ARG_NOT_SPECIFIED)
  49.                      cout << "t switch was not specified";
  50.                   else if (status == ARG_CLEAR)
  51.                      {
  52.                      cout << "t switch had a leading - ";
  53.                      cout << "with a value of " << value;
  54.                      }
  55.                   else if (status == ARG_SET)
  56.                      {
  57.                      cout << "t switch had a leading + ";
  58.                      cout << "with a value of " << value;
  59.                      }
  60.  
  61. (4)               status = parm.isSet(0,value);
  62.  
  63.                   if (status == ARG_NOT_SPECIFIED)
  64.                      cout << "no file name given";
  65.                   else
  66.                      cout << "file name: " << value;
  67.                }
  68.  
  69.  
  70.                The above code demonstrates the basic use of the arg class.
  71.                   (1) An instance of the object 'arg' is instantiated. The
  72.                       first two parameters are simply those passed to main.
  73.                       The last parameter (optional) is 1 if the switches are
  74.                       case sensitive, else 0. 0 is the default.
  75.  
  76.                   (2) You will need string space to store any values associated
  77.                       with a switch.
  78.  
  79.                   (3) This is the main member function used 'isSet'. This tests
  80.                       to see if a specified flag was given and if so, what was
  81.                       specified with it. The first argument is the char switch
  82.                       name to look for. The second parameter is the user allocated
  83.                       string space where any associated data is placed. The return
  84.                       value will be one of four things:
  85.                         ARG_NOT_SPECIFIED - this switch was not specified
  86.                         ARG_CLEAR - this switch had a leading minus sign (-).
  87.                         ARG_SET,  - this switch had a leading plus sign (+).
  88.                         ARG_NO_SWITCH - this parameter is unlabeled
  89.  
  90.                   (4) This is how you obtain a value for a parameter that
  91.                       does not use a leading + or -. This is known as an
  92.                       unlabeled or unnamed parameter.
  93.  
  94.                In addition, there is one other member function that may be
  95.                useful in debugging. It is called 'display'. This will display
  96.                to stdout all of the switches that it found.
  97.  
  98.  Limitations: - Values for switches cannot contain spaces. That is, there must
  99.                 be no spaces between the switch label and its associated
  100.                 text. In addition, the associated text cannot contain spaces.
  101.  
  102.               - If a two parameters have the same label, the last parameter
  103.                 specified will be the one used in isSet. Likewise, if more
  104.                 than one non-labeled parameter is given, isSet can only access
  105.                 the last one specified.
  106.  
  107.               - There are no limitations to the number of command line
  108.                 arguments that can be read by 'arg'. You will only be limited
  109.                 by the maximum command line allowed by DOS.
  110.  
  111.  
  112.  
  113.  
  114.  Created by:   Joe Kleinwaechter (j2k)
  115.  Date:         15-Feb-1991
  116.  
  117. \****************************************************************************/
  118.  
  119. enum argState
  120.    {   
  121.    ARG_NOT_SPECIFIED=0,
  122.    ARG_CLEAR,
  123.    ARG_SET,
  124.    ARG_NO_SWITCH
  125.    };
  126.  
  127. class argString
  128.    {
  129.    public:
  130.    argString *next;
  131.    char letter;
  132.    char *value;
  133.    argState state;
  134.    argString(char *);
  135.    ~argString();
  136.    };
  137.  
  138.    
  139. class arg
  140.    {
  141.    private:
  142.       argString *first;
  143.       argString *locate(char);
  144.       int caseSensitive;
  145.    public:
  146.       arg(int ac, char *av[],int c=0);
  147.       ~arg();
  148.       argState isSet(char, char []); //This determines whether a flag is set 
  149.       void display(void);      //This outputs the current flag settings to stdout
  150.    };
  151.  
  152.  
  153.    
  154. #endif
  155.  
  156.